﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using MyChannel.Library;
using MyChannel.DetailCategory;
using Newtonsoft.Json;
using MyChannel.Resources;

namespace MyChannel
{
    public partial class CategoryDetailPage2 : PhoneApplicationPage
    {
        MD5 md5 = new MD5();
        bool isload = false;
        string _title = "";
        public CategoryDetailPage2()
        {
            InitializeComponent();
        }
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            NavigationContext.QueryString.TryGetValue("name", out _title);
            title.Text = _title;
            Mylist.SelectedIndex = -1;
            LoadData();
            base.OnNavigatedTo(e);
        }
        private void LoadData()
        {
            if (!isload)
            {
                Progress.Visibility = System.Windows.Visibility.Visible;
                int page = 0;
                int size = 10;
                WebClient webclient = new WebClient();
                webclient.DownloadStringCompleted += webclient_DownloadStringCompleted;
                string timetamp = md5.GetTime();
                webclient.DownloadStringAsync(new Uri(AppResources.Server + string.Format(AppResources.GetDetailCategory, App.CategoryAction, page.ToString(), size.ToString(), App.OsName, App.OsVersion, App.DeviceID, md5.GetMD5(AppResources.MD5KEY + timetamp), timetamp, App.DeviceName)));
            }
        }
        void webclient_DownloadStringCompleted(object sender, System.Net.DownloadStringCompletedEventArgs e)
        {
            try
            {
                string json = e.Result;
                if (!string.IsNullOrEmpty(json))
                {
                    RootDetailCategoryObject rootDetailCategoryObject = JsonConvert.DeserializeObject<RootDetailCategoryObject>(json);
                    Mylist.DataContext = rootDetailCategoryObject.Data;
                    isload = true;
                    Progress.Visibility = System.Windows.Visibility.Collapsed;
                    network_error.Visibility = System.Windows.Visibility.Collapsed;
                }
            }
            catch
            {
                Progress.Visibility = System.Windows.Visibility.Collapsed;
                network_error.Visibility = System.Windows.Visibility.Visible;
            }
        }
        private void Mylist_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (Mylist.SelectedIndex < 0)
                return;
            App.URL = (Mylist.SelectedItem as DetailCategoryObject).Link;
            int type = int.Parse((Mylist.SelectedItem as DetailCategoryObject).Type);
            switch (type)
            {
                case 0:
                    {
                        NavigationService.Navigate(new Uri("/ViewVideoPage.xaml?type=0", UriKind.Relative));
                    } break;
                case 1:
                    {

                    } break;
                default: break;
            }
        }

        private void Reload_Click(object sender, RoutedEventArgs e)
        {
            network_error.Visibility = System.Windows.Visibility.Collapsed;
            isload = false;
            LoadData();
        }
    }
}